home *** CD-ROM | disk | FTP | other *** search
/ Disc to the Future 2 / Disc to the Future Part II Programmer's Reference (Wayzata Technology)(6013)(1992).bin / MAC / THINKC / 4_0 / IAC_STUF / IAC.C < prev    next >
Text File  |  1989-11-29  |  3KB  |  163 lines

  1. /* This file contains 'C' support for IAC (inter-application communication) on the
  2.    Macintosh.
  3.    For these routines to work, you must first install the IAC driver in your applications
  4.    resource file:
  5.            Use the project "IAC Driver.╣" to build the device driver.  Use ResEdit to
  6.            copy the device driver and it's DATA resource into the resource file.
  7.            Set the system heap and locked flags on each of these resources!
  8. */
  9.  
  10. #include "IAC.p"
  11.  
  12.  
  13. static int IACDriverNum;
  14.  
  15. struct IACBuffer {
  16.     int charAvail;
  17.     int bufSize;
  18.     char buffer[];
  19.     };
  20.  
  21. IACInit()
  22. {
  23.     int err;
  24.     
  25.     if ((err = OpenDriver((StringPtr)"\p.IAC-DRIVER", &IACDriverNum)) != noErr)
  26.         IACDriverNum = 0;
  27.     
  28.     return(err);
  29.     
  30. }
  31.  
  32. struct IACBuffer *
  33. IACRegister(appNum, bufSize)
  34. int appNum, bufSize;
  35. {
  36.     int csParams[11];
  37.  
  38.     *csParams = appNum;
  39.     *(csParams + 1) = bufSize;
  40.     
  41.     if ((IACDriverNum == 0) || Control(IACDriverNum, 1, (Ptr)&csParams) != noErr)
  42.         return(0L);
  43.     
  44.     /* Find my buffer */
  45.     Status(IACDriverNum, appNum, (Ptr)&csParams[0]);
  46.         
  47.     return(*((struct IACBuffer **)csParams));
  48. }
  49.  
  50. IACDeRegister(appNum)
  51. int appNum;
  52. {
  53.     int csParams[11];
  54.  
  55.     if (IACDriverNum == 0)
  56.         return(-1);
  57.  
  58.     *csParams = appNum;
  59.     
  60.     return(Control(IACDriverNum, 2, (Ptr)&csParams));
  61. }
  62.  
  63.  
  64. struct IACBuffer *
  65. IACLookup(appNum)
  66. int appNum;
  67. {
  68.     int csParams[11];
  69.  
  70.     if (IACDriverNum == 0)
  71.         return(0L);
  72.  
  73.     Status(IACDriverNum, appNum, (Ptr)&csParams[0]);
  74.         
  75.     return(*((struct IACBuffer **)csParams));
  76. }
  77.  
  78.  
  79. IACSend(buf, msg, msglen)
  80. struct IACBuffer *buf;
  81. char *msg;
  82. int msglen;
  83. {
  84.     register char *b, *m;
  85.     register int i;
  86.  
  87.     if (IACDriverNum == 0)
  88.         return(0);
  89.         
  90.     /* Is there enough space in the buffer? */
  91.     if (msglen > (buf->bufSize - buf->charAvail - 2)) {
  92.         /* DisplayCString("Output buffer overflow, nothing sent."); */
  93.         return(0);
  94.         }
  95.     
  96.     *((int *)(buf->buffer + buf->charAvail)) = msglen;
  97.     for (m = msg, b = buf->buffer + buf->charAvail + 2, i = msglen; i > 0; i--)
  98.         *b++ = *m++;
  99.         
  100.     buf->charAvail += msglen + 2;
  101.     
  102.     return(msglen);
  103. }
  104.  
  105.  
  106. IACGet(buf, msgBuf, max)
  107. struct IACBuffer *buf;
  108. char *msgBuf;
  109. int max;
  110. {
  111.     register char *s, *m;
  112.     register int i;
  113.     int c;
  114.     
  115.     /* Is there anything available? */
  116.     if (IACDriverNum == 0 || buf->charAvail == 0)
  117.         return(0);
  118.         
  119.     /* Return the first message */
  120.     if (*((int *)buf->buffer) < max)
  121.         c = i = *((int *)buf->buffer);
  122.     else {
  123.         i = max;
  124.         c = -1;
  125.         }
  126.         
  127.     for (s = buf->buffer + 2, m = msgBuf; i > 0; i--)
  128.         *m++ = *s++;
  129.     
  130.     /* Are there other messages in the buffer? */
  131.     if (buf->charAvail > *((int *)buf->buffer) + 2) {
  132.         i = buf->charAvail - *((int *)buf->buffer) - 2;
  133.         buf->charAvail = i;
  134.         for (s = buf->buffer, m = buf->buffer + *((int *)buf->buffer) + 2;
  135.              i > 0; i--)
  136.           *s++ = *m++;
  137.         }
  138.     else
  139.         buf->charAvail = 0;
  140.         
  141.     return(c);
  142.  
  143. }
  144.  
  145. IACClear(buf)
  146. struct IACBuffer *buf;
  147. {
  148.  
  149.     buf->charAvail = 0;
  150.  
  151. }
  152.  
  153.  
  154. IACClose()
  155. {
  156.     int err;
  157.     
  158.     if ((err = CloseDriver(IACDriverNum)) != noErr)
  159.         IACDriverNum = 0;
  160.     return(err);
  161.     
  162. }
  163.